/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package web;

import ejb.CategoryBeanLocal;
import ejb.ProductBean;
import ejb.ProductBeanLocal;
import entities.Category;
import entities.Product;
import java.util.ArrayList;
import java.util.List;
import javax.annotation.PostConstruct;
import javax.ejb.EJB;
import javax.faces.bean.ManagedBean;
import javax.faces.context.FacesContext;
import javax.servlet.http.HttpServletRequest;

/**
 *
 * @author Jesus
 */
@ManagedBean
public class MainBean {

    @EJB
    private ProductBeanLocal productBean;
    @EJB
    private CategoryBeanLocal categoryBean;
    private List<Product> products;
    private Short categoryId = null;

    public MainBean() {
        HttpServletRequest request = (HttpServletRequest) FacesContext.getCurrentInstance().getExternalContext().getRequest();
        products = new ArrayList<Product>();
        if (request.getParameter("category") != null) {
            categoryId = Short.valueOf(request.getParameter("category"));
        }
    }

    @PostConstruct
    public void init() {
        if (categoryId != null) {
            Category category = categoryBean.findById(categoryId);
            products = productBean.findByCategory(category);
        } else {
            products = productBean.findAll();
        }

    }

    public List<Product> getProducts() {
        return products;
    }

    public void setProducts(List<Product> products) {
        this.products = products;
    }

    public Short getCategoryId() {
        return categoryId;
    }

    public void setCategoryId(Short categoryId) {
        this.categoryId = categoryId;
    }
}
